package com.byb.server.web.rest;

import com.byb.server.service.dto.IllnessInfoForExcelDTO;
import com.byb.server.service.dto.IllnessInfoTypeDTO;
import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.IllnessInfoService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.IllnessInfoDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing IllnessInfo.
 */
@RestController
@RequestMapping("/api")
public class IllnessInfoResource {

    private final Logger log = LoggerFactory.getLogger(IllnessInfoResource.class);

    private static final String ENTITY_NAME = "illnessInfo";

    private final IllnessInfoService illnessInfoService;

    public IllnessInfoResource(IllnessInfoService illnessInfoService) {
        this.illnessInfoService = illnessInfoService;
    }

    /**
     * POST  /illness-infos : Create a new illnessInfo.
     *
     * @param illnessInfoDTO the illnessInfoDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new illnessInfoDTO, or with status 400 (Bad Request) if the illnessInfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/illness-infos")
    @Timed
    public ResponseEntity<IllnessInfoDTO> createIllnessInfo(@RequestBody IllnessInfoDTO illnessInfoDTO) throws URISyntaxException {
        log.debug("REST request to save IllnessInfo : {}", illnessInfoDTO);
        if (illnessInfoDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new illnessInfo cannot already have an ID")).body(null);
        }
        IllnessInfoDTO result = illnessInfoService.save(illnessInfoDTO);
        return ResponseEntity.created(new URI("/api/illness-infos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /illness-infos : Updates an existing illnessInfo.
     *
     * @param illnessInfoDTO the illnessInfoDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated illnessInfoDTO,
     * or with status 400 (Bad Request) if the illnessInfoDTO is not valid,
     * or with status 500 (Internal Server Error) if the illnessInfoDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/illness-infos")
    @Timed
    public ResponseEntity<IllnessInfoDTO> updateIllnessInfo(@RequestBody IllnessInfoDTO illnessInfoDTO) throws URISyntaxException {
        log.debug("REST request to update IllnessInfo : {}", illnessInfoDTO);
        if (illnessInfoDTO.getId() == null) {
            return createIllnessInfo(illnessInfoDTO);
        }
        IllnessInfoDTO result = illnessInfoService.save(illnessInfoDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, illnessInfoDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /illness-infos : get all the illnessInfos.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of illnessInfos in body
     */
    @GetMapping("/illness-infos")
    @Timed
    public ResponseEntity<List<IllnessInfoDTO>> getAllIllnessInfos(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of IllnessInfos");
        Page<IllnessInfoDTO> page = illnessInfoService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/illness-infos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /illness-infos/:id : get the "id" illnessInfo.
     *
     * @param id the id of the illnessInfoDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the illnessInfoDTO, or with status 404 (Not Found)
     */
    @GetMapping("/illness-infos/{id}")
    @Timed
    public ResponseEntity<IllnessInfoDTO> getIllnessInfo(@PathVariable Long id) {
        log.debug("REST request to get IllnessInfo : {}", id);
        IllnessInfoDTO illnessInfoDTO = illnessInfoService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(illnessInfoDTO));
    }

    /**
     * DELETE  /illness-infos/:id : delete the "id" illnessInfo.
     *
     * @param id the id of the illnessInfoDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/illness-infos/{id}")
    @Timed
    public ResponseEntity<Void> deleteIllnessInfo(@PathVariable Long id) {
        log.debug("REST request to delete IllnessInfo : {}", id);
        illnessInfoService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/illness-infos?query=:query : search for the illnessInfo corresponding
     * to the query.
     *
     * @param query the query of the illnessInfo search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/illness-infos")
    @Timed
    public ResponseEntity<List<IllnessInfoDTO>> searchIllnessInfos(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of IllnessInfos for query {}", query);
        Page<IllnessInfoDTO> page = illnessInfoService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/illness-infos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/illness-infos_for_product/{id}")
    @Timed
    public ResponseEntity<IllnessInfoTypeDTO> getAllIllnessInfosByProductId(@PathVariable Long id ,@RequestParam Long type) {
        log.debug("REST request to get a page of IllnessInfos by productId.");
        IllnessInfoTypeDTO illnessInfoTypeDTO = illnessInfoService.findAllByIdAndtypeId(id,type);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(illnessInfoTypeDTO));
    }

    @PostMapping("/illness-infos/save-all")
    @Timed
    public ResponseEntity<Boolean> saveAll(@RequestBody List<IllnessInfoDTO> illnessInfoDTOS) throws URISyntaxException {
        log.debug("REST request to save IllnessInfo : {}", illnessInfoDTOS);
        boolean result = illnessInfoService.saveAll(illnessInfoDTOS);
        return ResponseEntity.created(new URI("/api/illness-infos/save-all" ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, "全部安装成功"))
            .body(result);
    }
}
